package com.lyb.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.Dto.RolePermissionDto;
import com.lyb.mapper.RoleMapper;
import com.lyb.mapper.RolePermissionMapper;
import com.lyb.pojo.Role;
import com.lyb.pojo.RolePermission;
import com.lyb.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;
    @Override
    public PageInfo<Role> queryPage(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> res = roleMapper.queryPage();
        PageInfo<Role> pres = new PageInfo<>(res);
        return pres;
    }

    @Override
    public Boolean del(Long id) {
        return roleMapper.del(id);
    }

    @Override
    public Boolean update(Role role) {
        return roleMapper.update(role);
    }

//    @Override
//    public Boolean add(Role role) {
//        return roleMapper.add(role);
//    }

    @Override
    @Transactional
    public Boolean add(RolePermissionDto rolePermissionDto) {
        Role role = new Role();
        role.setName(rolePermissionDto.getName());
        roleMapper.add(role);
        Long id = role.getId();
        rolePermissionMapper.add(id,rolePermissionDto.getPermissionId());
        return true;
    }

    @Override
    public RolePermission findByRoleId(Long id) {
        return rolePermissionMapper.findByRoleId(id);
    }
}
